package com.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @Title: SetCookie
 * @Description:
 * @Auther: py
 * @Version: 1.0
 * @create 2023/12/25 10:10
 */
@WebServlet("/set")
public class SetCookie  extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 创建cookie
        Cookie cookie = new Cookie("user","admin");
        // 测试cookie的存储
        cookie = new Cookie("user", URLEncoder.encode("^小明_ 1-2&"));
        // 默认的存储时常是浏览器关闭  时间单位是 s
        cookie.setMaxAge(60*5);
        // 设置cookie存储的域
        // cookie.setDomain("web44.com");
        // 设置访问的路径
        cookie.setPath("/web/abc");
        // 将cookie发送给浏览器
        resp.addCookie(cookie);
        resp.getWriter().write("success!");
    }
}
